package com.qf.fronted.servlet;

import com.qf.base.BaseServlet;
import com.qf.commons.Data;
import com.qf.fronted.domain.Goods;
import com.qf.fronted.service.GoodService;
import com.qf.fronted.service.impl.GoodServiceImpl;
import com.qf.utils.CookieUtils;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.List;
import java.util.Objects;

/**
 * 用户首页中展示所有商品信息
 */
@WebServlet(value = "/good", name = "FrontedGoodServlet")
public class GoodServlet extends BaseServlet {

    private GoodService goodService = new GoodServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException{
        String methodName = req.getParameter("method");

        try {
            Method method = GoodServlet.class.getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this, req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void getGoods(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Goods> goodsList = goodService.getAll();
        sendJson(resp, goodsList);
    }

    private void getGoodsByItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String itemId = req.getParameter("itemId");
        List<Goods> goodsByItem = goodService.getGoodsByItem(Integer.parseInt(itemId));
        sendJson(resp, goodsByItem);
    }
}
